//
//  LeftDrawerViewController.m
//  DuckAndDoc
//
//  Created by 蓝水软件 on 2017/5/5.
//  Copyright © 2017年 苏州蓝水有限公司. All rights reserved.
//

#import "LeftDrawerViewController.h"
#import "LeftDrawTableViewCell.h"
#import "MapViewController.h"
#import "LoginRegisterViewController.h"
#import "WorkNoteListViewController.h"

@interface LeftDrawerViewController ()
@property (strong, nonatomic) IBOutlet UITableView *drawTable;
@property (nonatomic,strong) NSArray *toolCellModels;   //dataSource
@property (nonatomic,strong) UIButton *logOutButton;
@end

@implementation LeftDrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)UIConfig{
    self.view.backgroundColor = QXBlackColor;
}

- (void)setUI{
    [self UIConfig];
    self.drawTable.dataSource = self;
    self.drawTable.delegate = self;
    self.drawTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.drawTable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.logOutButton];
    [self.logOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(self.mm_drawerController.maximumLeftDrawerWidth);
        make.height.mas_equalTo(45);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark --buttonAction
- (void)logoutAction{
    [JCAlertView showTwoButtonsWithTitle:LocalizedStrFrom(@"AlertTitleOfChina") Message:LocalizedStrFrom(@"isSureLogoutOfChina") ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:LocalizedStrFrom(@"cancelOfChina") Click:^{
        //nil
    } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:LocalizedStrFrom(@"sureOfChina") Click:^{
        LoginRegisterViewController *loginRegisterController = [LoginRegisterViewController new];
        [UIApplication sharedApplication].keyWindow.rootViewController = loginRegisterController;
        [UserInfo logout];  //clearUserInfo
    }];
}

#pragma mark -- tableDelegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.toolCellModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeftDrawTableViewCell *cell = [LeftDrawTableViewCell cellWithTable:tableView];
    [cell cellWithModel:self.toolCellModels[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    LeftDrawModel *model = self.toolCellModels[indexPath.row];
    if ([model.text isEqualToString:@"workNote"]) {
        WorkNoteListViewController *workNoteController = [WorkNoteListViewController new];
        [self.delegate leftDrawer:self pushViewController:workNoteController];
        
    }else if ([model.text isEqualToString:@"location"]){
        MapViewController *mapController = [[MapViewController alloc] init];
        [self.delegate leftDrawer:self pushViewController:mapController];
    }
}

#pragma mark -- lazy load
- (NSArray *)toolCellModels{
    if (!_toolCellModels) {
        NSArray *toolTitles = @[@"workNote",@"overTime",@"moneyManager",@"location",@"ApiTest"];
        NSArray *toolIconNames = @[@"draw_note",@"draw_overTime",@"draw_moneyManager",@"draw_location",@"draw_ApiTest"];
        NSMutableArray *tempArr = [NSMutableArray new];
        for (int index = 0;  index < toolTitles.count; index++) {
            LeftDrawModel *model = [LeftDrawModel new];
            model.text = toolTitles[index];
            model.icon = [UIImage imageNamed:toolIconNames[index]];
            [tempArr addObject:model];
        }
        _toolCellModels = [[NSArray alloc] initWithArray:tempArr];
    }
    return _toolCellModels;
}

- (UIButton *)logOutButton{
    if (_logOutButton == nil) {
        _logOutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _logOutButton.backgroundColor = [UIColor clearColor];
        [_logOutButton setTitle:LocalizedStrFrom(@"logoutOfChina") forState:UIControlStateNormal];
        [_logOutButton setTitleColor:QXWhiteColor forState:UIControlStateNormal];
        [_logOutButton addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logOutButton;
}
@end
